package com.paic.custserv.web.controller.manager;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.paic.custserv.common.ResultMap;
import com.paic.custserv.common.enums.StatusEnum;
import com.paic.custserv.domain.CussFaq;
import com.paic.custserv.domain.CussFaqCategder;
import com.paic.custserv.service.ICussFaqCategderService;
import com.paic.custserv.service.ICussFaqService;

/**
 * 常见问题管理
 */
@RequestMapping("manager/faq")
@Controller
public class ManagerFaqController {
	
	private static final Logger logger = (Logger) LoggerFactory.getLogger(ManagerFaqController.class);
	
	@Autowired
	private ICussFaqCategderService cussFaqCategderService;
	
	@Autowired
	private ICussFaqService cussFaqService;
	
	/**
	 *  首页 "常见问题管理" 跳转
	 */
	@RequestMapping(value="/transFAQ", method = RequestMethod.GET)
	public ModelAndView classDetail(HttpServletRequest request){
		return new ModelAndView("/page/manager/faq_category");
	}
	/**
	 * 常见问题分类    初始化树  异步加载
	 */
	@RequestMapping(value="/category/query", method = RequestMethod.GET)
	public @ResponseBody ResultMap faqQuery(HttpServletRequest request) {
		List<Map<String,Object>> resultList = null;
		try{
			Integer parentId = StringUtils.isEmpty(request.getParameter("idFaqCategory")) ? null : 
				Integer.valueOf(request.getParameter("idFaqCategory"));
			resultList = cussFaqCategderService.getCussFaqCategory(parentId);
//			logger.info("查询结果resultMap={}",resultList);
		}catch(Exception e) {
			logger.error("初始化常见问题分类树异常信息>>>{}",e);
			return new ResultMap(StatusEnum.fail,new ArrayList<Map<String,Object>>());
		}
		return new ResultMap(StatusEnum.success,resultList);
	}
	
	/**
	 * 点击节点进入展示问题页面
	 */
	@RequestMapping(value="/show", method = RequestMethod.GET)
	public @ResponseBody ResultMap faqShow(HttpServletRequest request) {
		List<CussFaq> resultList = null;
		try{
			String levels = request.getParameter("levels");
			resultList = cussFaqService.getFaqByLevels(levels);
			logger.info("查询结果resultList={}",resultList);
		}catch(Exception e) {
			logger.error("展示问题页面异常信息>>>{}",e);
			return new ResultMap(StatusEnum.fail,new ArrayList<CussFaq>());
		}
		return new ResultMap(StatusEnum.success,resultList);
	}
	
	@RequestMapping("save")
	public ResultMap faqSave(@RequestBody CussFaq model) {
		try{
			
		}catch(Exception e) {
			e.printStackTrace();
		}
		return new ResultMap(StatusEnum.fail);
	}
	
	@RequestMapping("update")
	public ResultMap faqUpdate(@RequestBody CussFaq model) {
		try{
			
		}catch(Exception e) {
			e.printStackTrace();
		}
		return new ResultMap(StatusEnum.fail);
	}
	
	/**
	 * 保存常见问题分类
	 */
	@RequestMapping(value="/category/save",method = RequestMethod.POST)
	@ResponseBody
	public ResultMap categorySave(@RequestBody CussFaqCategder model) {
		try{
			cussFaqCategderService.save(model);
			return new ResultMap(StatusEnum.success);
		}catch(Exception e) {
			logger.info(">>>保存常见问题类目出错:");
			e.printStackTrace();
		}
		return new ResultMap(StatusEnum.fail);
	}
	
	/**
	 * 修改常见问题类目
	 * @author 胡启萌
	 * @date 2015年8月26日 上午11:41:56
	 */
	@RequestMapping("category/update")
	@ResponseBody
	public ResultMap categoryUpdate(@RequestBody CussFaqCategder model) {
		try{
			cussFaqCategderService.update(model);
			return new ResultMap(StatusEnum.success);
		}catch(Exception e) {
			logger.error(">>>修改常见问题类目出错:{}",e);
		}
		return new ResultMap(StatusEnum.fail);
	}
	
	/**
	 * 查询常见类目
	 * @author 胡启萌
	 * @date 2015年8月26日 上午11:38:28
	 */
//	@RequestMapping("/category/query")
//	@ResponseBody
//	public ResultMap categoryQuery(@RequestBody CussFaqCategder model) {
//		try{
//			List<CussFaqCategder> list = cussFaqCategderService.queryList(model);
//			return new ResultMap(StatusEnum.success, list);
//		}catch(Exception e) {
//			logger.info(">>>查询常见类目:");
//			e.printStackTrace();
//		}
//		return new ResultMap(StatusEnum.fail);
//	}
	

}
